package com.hxh.learn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hxh.common.unification.ResultOutcome;
import com.hxh.learn.model.SysPermissions;
import com.hxh.learn.model.SysUser;
import com.hxh.learn.service.ISysPermissionsService;
import com.hxh.learn.vo.SysPermissionsInsertVo;
import com.hxh.learn.vo.SysPermissionsSearchVo;
import com.hxh.learn.vo.SysPermissionsUpdateVo;
import com.hxh.learn.vo.SysUserSearchVo;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

/**
 * <p>
 * 系统权限 前端控制器
 * </p>
 *
 * @author hxh
 * @since 2023-03-02
 */
@RequestMapping("/learn/sysPermissions")
@RestController
public class SysPermissionsController {
    @Resource
    private ISysPermissionsService permissionsService;
    @PostMapping("/add")
    @ApiOperation("添加权限")
    public ResultOutcome<String> add(@Validated @RequestBody SysPermissionsInsertVo sysPermissionsInsertVo){
        SysPermissions sysPermissions = sysPermissionsInsertVo.convert();
        boolean save = permissionsService.save(sysPermissions);
        if (!save){
            return ResultOutcome.fileResult("添加权限失败");
        }
        return ResultOutcome.successResult();
    }


    @PostMapping("/update")
    @ApiOperation("修改权限")
    public ResultOutcome<String> update(@Validated @RequestBody SysPermissionsUpdateVo permissionsUpdateVo){
        SysPermissions sysPermissions = permissionsUpdateVo.convert();
        boolean save = permissionsService.updateById(sysPermissions);
        if (!save){
            return ResultOutcome.fileResult("修改权限失败");
        }
        return ResultOutcome.successResult();
    }

    @GetMapping("/detail")
    @ApiOperation("详情")
    public ResultOutcome<SysPermissions> detail(String id) {
        SysPermissions sysPermissions = permissionsService.getById(id);
        return ResultOutcome.successResult(sysPermissions);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户")
    public ResultOutcome<Boolean> delete(String id) {
        boolean remove = permissionsService.removeById(id);
        if (!remove){
            ResultOutcome.fileResult("删除权限失败");
        }
        return ResultOutcome.successResult();
    }

    @PostMapping("/list")
    @ApiOperation("分页查询")
    public ResultOutcome<Page<SysPermissions>> list(@RequestBody SysPermissionsSearchVo sysPermissionsSearchVo) {
        Page<SysPermissions> sysPermissionsPage = permissionsService.queryPermissionsList(sysPermissionsSearchVo);
        return ResultOutcome.successResult(sysPermissionsPage);
    }


}
